package com.zz.comframe.servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Date;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.zz.comframe.CommonDataProcesser;
import com.zz.comframe.Page;
import com.zz.comframe.properties.PropertiesPool;

public class GetComDataList extends HttpServlet { // 提供零散数据访问，json形式返回记录

	public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

		this.doPost(request, response);
	}

	@SuppressWarnings("deprecation")
	public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

		response.setContentType("text/html; charset=utf-8");
		PrintWriter out = response.getWriter();

		try {
			int dpage = 1;
			int limit = 15;
			String where = "";
			// try {
			// String str = request.getParameter("start");
			dpage = Integer.parseInt(request.getParameter("page"));
			// start = (limit * (dpage-1));
			limit = Integer.parseInt(request.getParameter("rows"));
			// } catch (Exception ex) {

			// }
			String sort = request.getParameter("sidx");
			sort = sort != null && !sort.equals("id") && !sort.equals("") ? sort : null;
			String dir = request.getParameter("sord");
			// String grid = request.getParameter("grid"); //呈现与那张表
			String term = request.getParameter("term"); // 学期，必选条件
			Date d = null;
			if (term != null && !"".equals(term))
				d = Date.valueOf(term);
			else {
				out.print("{success:false,errors:[{id:\"ExcuteSQL\",msg:\"term为空\"}]}");
				return;
			}
			if (d == null) {
				java.util.Date d1 = new java.util.Date(System.currentTimeMillis());
				if (d1.getMonth() > 6)
					d1.setMonth(9); // 设置成20xx-3-1或20xx-9-1二种格式
				else
					d1.setMonth(2);
				d1.setDate(1);
				term = String.format("%1$tY-%1$tm-%1$td", d1);
			} else
				term = String.format("%1$tY-%1$tm-%1$td", d);

			// 需要解此——{"groupOp":"AND","rules":[{"field":"col1","op":"bw","data":"23"},{"field":"col2","op":"bw","data":"2"}]}
			String filters = request.getParameter("filters") == null || "".equals(request.getParameter("filters")) ? null : request.getParameter("filters");
			// filters = tmp==null || "".equals(tmp)?null:new
			// String(tmp.getBytes("iso-8859-1"), "utf-8");
			if (filters != null)
				where = PropertiesPool.getJQGridWhere(filters);
			if ( where!=null&&!"".equals(where))
				where = "where " + where.trim().replaceAll("^where", PropertiesPool.getCommonpro("TERM_COLUMN_NAME") + "='" + term + "' and (") + ") ";
			else
				where = "where " + PropertiesPool.getCommonpro("TERM_COLUMN_NAME") + "='" + term +"'";
			
			CommonDataProcesser dp = new CommonDataProcesser();
			String sql = PropertiesPool.getCommonpro("SQL_SELECT_PERINFO") + where;
			Page p = dp.PageQuery(sql, dpage, limit, sort, dir); // sql分页查询
			String str = p.PrintJSONString();
			out.print(str);

			out.flush();
			out.close();
			// form.reset(mapping, request);
		} catch (Exception e) {
			out.print("{success:false,errors:[{id:\"ExcuteSQL\",msg:\"数据访问出错\"}]}");
			System.err.println(e);
		}
	}
}
